// export let BASE_URL="https://d-gangzhong.bigchun.com/"
export let BASE_URL = "https://d-gangzhong.bigchun.com/";
// // #ifdef APP-PLUS
//    BASE_URL="https://d-gangzhong.bigchun.com/"  //测试
// // #endif
/**
 * @description 请求方法
 * @param option {Object}
 * @param option.header {String} 请求的Content-Type 默认值 ”application/json“
 * @param option.method {String} 请求类型 默认post
 * @param option.data {Object} 请求参数 默认 {}
 * @param option.url {String} 请求地址 必传没有默认值
 * @param option.data.loading {Boolean}  是否显示loading 默认不显示
 * @param option.data.loadingText {String} 显示loading时提示文字 默认加载中
 *
 */
let is = true;
export const request = (option) => {
  return new Promise((resolve, reject) => {
    const loading = option.data && option.data.loading;
    if (loading) {
      uni.showLoading({
        mask: true,
        title: option.data.loadingText || "加载中",
      });
    }
    if (option.data) {
      delete option.data.loading;
      delete option.data.loadingText;
    }
    const header = {
      token: uni.getStorageSync("token") ? uni.getStorageSync("token") : "",
      "Content-Type": option.header || "application/json",
    };
    uni.request({
      url: BASE_URL + option.url,
      method: option.method || "post",
      data: option.data || {},
      timeout: 60000,
      header: header,
      success: (res) => {
        if (res.statusCode == 401) {
          // logOut()
          resolve(res.data);
          uni.switchTab({
            url: "/pages/my/index",
          });
        } else if (res.statusCode != 200 && res.statusCode != 405) {
          uni.showToast({
            title:
              res.statusCode == 400 && res.data?.message.length < 30
                ? res.data.message
                : "请求失败",
            icon: "none",
          });
        } else {
          if (res.data.code == 401) {
            //token失效,请重新登录
            // logOut()
            resolve(res.data);
          } else {
            resolve(res.data);
          }
        }
      },
      fail: (err) => {
        uni.showToast({
          title: "服务器异常",
          icon: "none",
        });
        reject(err);
      },
      complete: () => {
        if (loading) {
          setTimeout(() => {
            uni.hideLoading();
          }, 300);
        }
      },
    });
  });
};
function logOut(msg = "请先登录") {
  uni.removeStorageSync("token");
  uni.reLaunch({
    url: "/pages/login/login",
  });
}
export default BASE_URL;
